package cn.xianshu.org.controller;

import cn.xianshu.basic.util.AxiosResult;
import cn.xianshu.basic.util.PageList;
import cn.xianshu.org.domain.SystemsType;
import cn.xianshu.org.query.SystemsTypeQuery;
import cn.xianshu.org.service.ISystemsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/systemType")
public class    StstemsTypeController {
    /**
     * 注入service层对象
     */
    @Autowired
    private ISystemsTypeService systemsTypeService;

    /**
     * 查询一条数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public SystemsType findOne(@PathVariable("id") Long id){
        return systemsTypeService.findById(id);
    }

    /**
     * 查询所有数据
     * @return
     */
    @GetMapping
    public List<SystemsType> findAll(){
        return systemsTypeService.findAll();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AxiosResult delete(@PathVariable("id") Long id){
        try {
            systemsTypeService.delete(id);
            return AxiosResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AxiosResult.me().setMessage("系统繁忙，请稍后重试。。。。");
        }
    }

    @PatchMapping
    public AxiosResult Bydelete(@RequestBody List<Long> ids){
        try {
            systemsTypeService.Bydelete(ids);
            return AxiosResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AxiosResult.me().setMessage("系统繁忙，请稍后重试。。。。");
        }
    }

    /**
     * 添加修改
     * @param
     * @return
     */
    @PutMapping
    public AxiosResult addAndUpdate(@RequestBody SystemsType systemsType){
        try {
            if (systemsType.getId()==null){
                systemsTypeService.insert(systemsType);
            } else {
                systemsTypeService.update(systemsType);
            }
            return AxiosResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AxiosResult.me().setMessage("系统繁忙，请稍后重试。。。。");
        }
    }

    /**
     * 分页查询
     * @param query
     * @return
     */
    @PostMapping
    public PageList<SystemsType> queryPage(@RequestBody SystemsTypeQuery query){
        return systemsTypeService.pageQuery(query);
    }

    @GetMapping("/tree")
    public List<SystemsType> queryTree(){
        return systemsTypeService.queryTree();
    }
}
